package com.icode.web.controller.house;

import com.icode.core.dto.house.BannerForAppDTO;
import com.icode.core.model.house.*;
import com.icode.core.dto.house.EquipmentDTO;
import com.icode.core.dto.house.HouseInfoDTO;
import com.icode.core.model.house.EquipmentType;
import com.icode.core.shared.DataToJson;
import com.icode.core.shared.StringUtils;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: lb
 * Date: 15-11-13
 * Time: 下午5:02
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/bannerforapp")
public class BannerForAppController {

    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public ModelAndView show() {
        BannerForApp bannerForApp = houseService.findBannerForApps();
        BannerForAppDTO bannerForAppDTO = new BannerForAppDTO();

        if (bannerForApp != null) {
            bannerForAppDTO = BannerForAppDTO.toDTO(bannerForApp);
        }
        return new ModelAndView("back/houseBase/bannerForApp", "bannerForApp", bannerForAppDTO);
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String saveOrUpdateBannerForApp(BannerForAppDTO bannerForAppDTO) {
        String guid = bannerForAppDTO.getGuid();
        if (StringUtils.isEmpty(guid)) {
            houseService.saveOrUpdateBannerForApp(bannerForAppDTO);
        } else {
            houseService.upDataBannerForApp(bannerForAppDTO);
        }
        return "redirect:/bannerforapp/form";
    }

    @RequestMapping(value = "delImg", method = RequestMethod.POST)
    public void deleteUploadImage(String imgGuid, HttpServletResponse response) {
        houseService.deleteFirstBanner(imgGuid);
        String content = "success";
        DataToJson.DataToJson(content, response);
    }

    @RequestMapping(value = "delImg2", method = RequestMethod.POST)
    public void deleteUploadImage2(String imgGuid, HttpServletResponse response) {

        houseService.deleteImageForBanner(imgGuid);
        String content = "success";
        DataToJson.DataToJson(content, response);
    }
}
